//
//  CityPickView.m
//  StraightTrain
//
//  Created by mk on 15/11/25.
//  Copyright (c) 2015年 mk. All rights reserved.
//

#import "CityPickView.h"

@implementation CityPickView
{
   // NSMutableArray *  arrProvince;//省份
    //NSMutableArray * arrCity;//城市
}

-(id)init
{
   self =  [super init];
    if (self)
    {
       [self initDataView];
    }

    return self;
}


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self initDataView];
    }
    return self;
}

-(void)awakeFromNib
{
    [super awakeFromNib];
    
    [self initDataView];
    
}


-(void)initDataView
{
    self.backgroundColor = [UIColor whiteColor];
    pickerCity.dataSource = self;
    pickerCity.delegate = self;
    self.selectCity = 0;
    self.selectProvince = 0;
    [self loadView];
}


-(void)loadView
{
    UIView * viewTitle = [[UIView alloc]initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, 44)];
    viewTitle.backgroundColor = UIColorFromRGB(0x1F,0xab,0xa0);
    [self addSubview:viewTitle];
    
    self.cityTitle = [[UILabel alloc]initWithFrame:CGRectMake(70, 0,viewTitle.width-(70*2), 44)];
    self.cityTitle.text = @"选择城市";
    self.cityTitle.textColor = [UIColor whiteColor];
    self.cityTitle.font = [UIFont systemFontOfSize:15];
    [self.cityTitle setTextAlignment:NSTextAlignmentCenter];
    [viewTitle addSubview:self.cityTitle];
    
    UIButton * btnOK = [[UIButton alloc]initWithFrame:CGRectMake(viewTitle.width-60, 0, 60, 44)];
    [btnOK addTarget:self action:@selector(confirmChangeData:) forControlEvents:UIControlEventTouchUpInside];
    [btnOK setTitle:@"确定" forState:UIControlStateNormal];
    [viewTitle addSubview:btnOK];
    
    UIButton * btnCencel = [[UIButton alloc]initWithFrame:CGRectMake(0, 0,60, 44)];
    [btnCencel addTarget:self action:@selector(canelChangeData:) forControlEvents:UIControlEventTouchUpInside];
    [btnCencel setTitle:@"取消" forState:UIControlStateNormal];
    
    [viewTitle addSubview:btnCencel];
    
    [self setDataPick];
}

-(void)setDataPick
{
    if (!pickerCity)
    {
        pickerCity = [[UIPickerView alloc] init];
        [pickerCity setBackgroundColor:[UIColor whiteColor]];
        [self addSubview:pickerCity];
        [pickerCity reloadAllComponents];
        
        NSDictionary * views = NSDictionaryOfVariableBindings(pickerCity);
        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0@1000-[pickerCity]-0@1000-|"
                                                                     options:0
                                                                     metrics:nil
                                                                       views:views]];
        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-44@1000-[pickerCity]-0@1000-|"
                                                                     options:0
                                                                     metrics:nil
                                                                       views:views]];
    }
}




//以下3个方法实现PickerView的数据初始化
//确定picker的轮子个数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}
//确定picker的每个轮子的item数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0)
    {//省份个数
        return [gProjConfig.arrCity count];
    }
    else
    {//市的个数
        if (gProjConfig.arrCity.count>0)
        {
            
          NSMutableDictionary * dicProvince = [gProjConfig.arrCity objectAtIndex:self.selectProvince];
          NSMutableArray * arrayCity = [dicProvince objectForKey:@"subCity"];
          return [arrayCity count];
        }
        return 0;
    }
}

//确定每个轮子的每一项显示什么内容
#pragma mark 实现协议UIPickerViewDelegate方法
-(NSString *)pickerView:(UIPickerView *)pickerView
            titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component == 0)
    {//选择省份名
        NSMutableDictionary * dicState = [gProjConfig.arrCity objectAtIndex:row];
        return [dicState objectForKey:@"state"];
    }
    else
    {
        //选择市名
       // DLog(@"=ldec(long)tProvince=%ld",(long)self.selectProvince);
        NSMutableDictionary * dicState = [gProjConfig.arrCity objectAtIndex:self.selectProvince];
        NSArray * arr = [dicState objectForKey:@"subCity"];
        NSMutableDictionary * dic = [arr objectAtIndex:row];
        return [dic objectForKey:@"city"];
    }
}

#pragma mark  UIPickerViewDelegate
//监听轮子的移动
- (void)pickerView:(UIPickerView *)pickerView
      didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component == 0)//第一列
    {
        self.selectProvince = [pickerView selectedRowInComponent:component];
        self.selectCity = 0;
        
        //重点！更新第二个轮子的数据
        [pickerCity reloadComponent:1];
        [pickerCity selectRow:0 inComponent:1 animated:YES];
    }
    else
    {
        self.selectCity = [pickerView selectedRowInComponent:component];
    }
    
    NSMutableDictionary * dicProvince = [gProjConfig.arrCity objectAtIndex:self.selectProvince];
    NSString * strProvince = [dicProvince objectForKey:@"state"];
    
    NSMutableArray * arrayCity = [dicProvince objectForKey:@"subCity"];
    NSMutableDictionary * dicCity = [arrayCity objectAtIndex:self.selectCity];
    NSString * strCity = [dicCity objectForKey:@"city"];
    
    
    if ([strProvince isEqualToString:strCity])
    {
        self.cityTitle.text = [NSString stringWithFormat:@"%@市",strCity];
    }else
    {
        self.cityTitle.text = [NSString stringWithFormat:@"%@省/%@市",strProvince,strCity];
    }
    
    if ([self.selectDelegate respondsToSelector:@selector(selectPickData:provinceData:)])
    {
        [self.selectDelegate selectPickData:strCity provinceData:strProvince];
    }
}



-(void)canelChangeData:(id)sender
{
    if ([self.selectDelegate respondsToSelector:@selector(citytoolBarCanelClick:)])
    {
        [self.selectDelegate citytoolBarCanelClick:sender];
    }
    
    if ([self.selectDelegate respondsToSelector:@selector(showCityPickerData:)])
    {
        [self.selectDelegate showCityPickerData:NO];
    }
}

-(void)confirmChangeData:(id)sender
{
    if ([self.selectDelegate respondsToSelector:@selector(citytoolBarDoneClick:)])
    {
        [self.selectDelegate citytoolBarDoneClick:sender];
    }
    
    if ([self.selectDelegate respondsToSelector:@selector(showCityPickerData:)])
    {
        [self.selectDelegate showCityPickerData:NO];
    }
}





/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


@end
